package cn.dnui_wwt524.dnuifood_16110100524.db;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class RecordDBOperation {
    private SQLiteDatabase db;
    public RecordDBOperation(RecordSQLiteOpenHelper helper){
        db=helper.getWritableDatabase();
    }


    public void insertData(String tempName){
        db.execSQL("insert into records(name) values('"+ tempName +"')");
    }
    public List queryData(String tempName){
        List list=new ArrayList<>();
        Cursor cursor=db.rawQuery("select id as _id,name from records where name like '%" + tempName + "%' order by id desc",null);
        while(cursor.moveToNext()){
            Map map= new HashMap<>();
            map.put("name",cursor.getString(cursor.getColumnIndex("name")));
            list.add(map);
        }
        return list;
    }
    public boolean hasData(String tempName){
        Cursor cursor = db.rawQuery("select id as _id,name from records where name =?",new String[]{tempName});
        return cursor.moveToNext();
    }

    public void deleteData(){
        db.execSQL("delete from records");
    }

    public void closedb(){
        db.close();
    }
}